<?php
class Controller_App_Davshop extends Controller_App_Template
{

    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/davshop_list";
        parent::__construct($req, $res);
    }

    public function action_index ()
    {
        $this->action_list();
    }

    public function action_goods($shopId, $page = 1)
    {
        $page  = max($page, 1);
        $limit = 20;
        $start = ($page - 1 ) * $limit;

        $order = Arr::get($this->request->query(), 'order', 'countsLike');
        $goods = App_Goods::listByShop($shopId, array($order, 'desc'), $start, $limit);

        $this->template->set('shop', App_Shop::basicInfo ($shopId));
        $this->template->set('goodsItems', $goods);
        $this->template->set('order', $order);
        $this->template->set_filename('smarty:app/davshop_goods');
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '宝贝详情', 'share', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    public function action_list()
    {
        // banner
        $banner = new App_Shop_Banner();
        $banners = $banner->banners();

        // shops
        $shops = App_Shop::items();

        $this->template->set_filename('smarty:app/davshop_list');
        $this->template->set('shops', $shops);
        $this->template->set('banners', $banners);
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '大V店', 'share', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

    public function action_detail($nid)
    {
        // 增加访问数
        App_Shop::countsInc($nid, 'countsView', 1);

        $shop = App_Shop::basicInfo ($nid);
        $goods= App_Goods::listByShop($nid, array('created', 'desc'), 0, 4);
        $shop['isLiked'] = $this->_user->hasAction('shop', $nid);

        $this->template->set('shop', $shop);
        $this->template->set('goodsItems', $goods);
        $this->template->set_filename('smarty:app/davshop_detail');
        $this->template->set('shareTitle', App_Util::buildShareContent($shop['shopName']));
        $this->template->set('shareDesc', App_Util::buildShareContent($shop['homeDesc']));
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '大V店', 'share', 'block', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }

}
